package gui.customizer.panes.netfilter;

import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import core.models.PolicyRule;
import core.models.Rule;

import net.miginfocom.swing.MigLayout;

public class DefaultFilterPolicyPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3866793874445022336L;
	private JComboBox cmbInputPolicy;
	private JComboBox cmbOutputPolicy;
	private JComboBox cmbForwardPolicy;

	public DefaultFilterPolicyPanel() {
		this.setBorder(BorderFactory.createTitledBorder("Default Filter Policy"));
		this.setLayout(new MigLayout("", "[][]20[][]20[][]", "[]"));
		
		JLabel lblInput = new JLabel("INPUT");
		this.add(lblInput, "cell 0 0,alignx trailing");
		
		cmbInputPolicy = new JComboBox();
		this.add(cmbInputPolicy, "cell 1 0,growx");
		
		JLabel lblOutput = new JLabel("OUTPUT");
		this.add(lblOutput, "cell 2 0,alignx trailing");
		
		cmbOutputPolicy = new JComboBox();
		this.add(cmbOutputPolicy, "cell 3 0,growx");
		
		JLabel lblForward = new JLabel("FORWARD");
		this.add(lblForward, "cell 4 0,alignx trailing");
		
		cmbForwardPolicy = new JComboBox();
		this.add(cmbForwardPolicy, "cell 5 0,growx");
	}
	
	public void setAvailablePolicies(String[] policies) {
		cmbInputPolicy.setModel(new DefaultComboBoxModel(policies));
		cmbInputPolicy.setSelectedIndex(-1);
		cmbOutputPolicy.setModel(new DefaultComboBoxModel(policies));
		cmbOutputPolicy.setSelectedIndex(-1);
		cmbForwardPolicy.setModel(new DefaultComboBoxModel(policies));
		cmbForwardPolicy.setSelectedIndex(-1);
	}
	
	public void setPolicies(List<Rule> policyRuleSet) {
		for (Rule r : policyRuleSet) {
			if (r.getChain().equals("INPUT")) {
				cmbInputPolicy.setSelectedItem(r.getOptions().get(""));
			} else if (r.getChain().equals("OUTPUT")) {
				cmbOutputPolicy.setSelectedItem(r.getOptions().get(""));
			} else if (r.getChain().equals("FORWARD")) {
				cmbForwardPolicy.setSelectedItem(r.getOptions().get(""));
			}
		}
	}
	
	public List<Rule> getPolicies() {
		List<Rule> policies = new ArrayList<Rule>(3);
		policies.add(new PolicyRule("filter", "INPUT", (String) cmbInputPolicy.getSelectedItem()));
		policies.add(new PolicyRule("filter", "OUTPUT", (String) cmbOutputPolicy.getSelectedItem()));
		policies.add(new PolicyRule("filter", "FORWARD", (String) cmbForwardPolicy.getSelectedItem()));
		return policies;	
	}
}
